<?php

/**
 * AbstractController class.
 *
 *
 * @package    lib
 * @subpackage lib_controller
 * @author     Juan Carlos Jarquin (zetta)
 * @author     $LastChangedBy$
 * @version    1.1, SVN: $Id$
 */
class AbstractController implements ControllerInterface
{
  /**
   * @var Response
   */
  protected $response;
  
  /**
   * @var Request
   */
  protected $request;
  
  /**
   * @var View
   */
  protected $view;
  
  /**
   * Configuration Parameters
   *
   * @var SimpleXmlElement
   */
  protected $config;
  
  /**
   * Constructor Class
   * @return AbstractController
   */
  public function AbstractController()
  {
    $this->request = Request::getInstance();
    $this->response = Response::getInstance();
    $this->view = View::getInstance();
  }
  
  /**
   * @return Request
   */
  public function getRequest()
  {
    return $this->request;
  }
  
  public function getResponse()
  {
    return $this->response;
  }
  
  public function dispatch($action)
  {
    $reflectionObject = new ReflectionClass($this);
    if ($reflectionObject->hasMethod($action))
    {
      $reflectionMethod = new ReflectionMethod($this, $action);
      $reflectionMethod->invoke($this);
    }
    else if ($reflectionObject->hasMethod('default' . $this->request->getActionKey()))
    {
      $reflectionMethod = new ReflectionMethod($this, 'default' . $this->request->getActionKey());
      $reflectionMethod->invoke($this);
    }
    else
      throw new DispatcherException('Cant Dispatch cause no default exists');
      
      
    $this->view->prepare()->render();  
  }
  
  public function preDispatch()
  {

  }
  
  public function postDispatch()
  {

  }
  
  public function prepare()
  {
  }

}
